import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

# 支持中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
# 支持负数
plt.rcParams['axes.unicode_minus'] = False

def create_3d_cartesian_system(x_range=(-5, 5), y_range=(-5, 5), z_range=(-5, 5), figsize=(10, 8)):
    """
    创建三维直角坐标系
    """
    fig = plt.figure(figsize=figsize)
    ax = fig.add_subplot(111, projection='3d')
    
    # 设置坐标轴范围
    ax.set_xlim(x_range[0], x_range[1])
    ax.set_ylim(y_range[0], y_range[1])
    ax.set_zlim(z_range[0], z_range[1])
    
    # 设置标签
    ax.set_xlabel('X轴', fontsize=12)
    ax.set_ylabel('Y轴', fontsize=12)
    ax.set_zlabel('Z轴', fontsize=12)
    
    # 设置刻度
    ax.set_xticks(np.arange(x_range[0], x_range[1]+1, 2))
    ax.set_yticks(np.arange(y_range[0], y_range[1]+1, 2))
    ax.set_zticks(np.arange(z_range[0], z_range[1]+1, 2))
    
    # 标记原点
    ax.scatter(0, 0, 0, color='black', s=50)
    ax.text(0.2, 0.2, 0.2, 'O', fontsize=12, fontweight='bold')
    
    ax.set_title('三维直角坐标系', fontsize=16, pad=20)
    
    return fig, ax

if __name__ == "__main__":    
    # 三维直角坐标系
    fig2, ax2 = create_3d_cartesian_system()
    # 添加一个示例点
    ax2.scatter(2, 3, 4, color='red', s=100, label='点(2, 3, 4)')
    ax2.legend()
    plt.show()